module anem_mod

  use container
  use obs_base_mod
  use params_mod

  implicit none

  type, extends(obs_station_type) :: anem_tower_type
    type(linked_list_type), pointer :: records => null()
  contains
    procedure :: init => anem_tower_init
    final :: anem_tower_final
  end type anem_tower_type

  type, extends(obs_static_record_base_type) :: anem_record_type
    type(anem_tower_type), pointer :: tower
    real :: h      = real_missing_value 
    real :: wd_avg = real_missing_value ! Wind direction (deg)
    real :: wd_std = real_missing_value
    real :: wd_min = real_missing_value
    real :: wd_max = real_missing_value
    real :: ws_avg = real_missing_value ! Wind speed (m s-1)
    real :: ws_std = real_missing_value
    real :: ws_min = real_missing_value
    real :: ws_max = real_missing_value
    real :: ua_avg = real_missing_value ! Wind u component (m s-1)
    real :: va_avg = real_missing_value ! Wind v component (m s-1)
    real :: ta_avg = real_missing_value ! Temperature (degC)
    real :: ta_std = real_missing_value
    real :: ta_min = real_missing_value
    real :: ta_max = real_missing_value
    real :: p_avg  = real_missing_value ! Pressure (hPa)
    real :: p_std  = real_missing_value
    real :: p_min  = real_missing_value
    real :: p_max  = real_missing_value
    real :: rh     = real_missing_value ! Relative humidity (%)
  end type anem_record_type

contains

  subroutine anem_tower_init(this, name, lon, lat, z)

    class(anem_tower_type), intent(inout) :: this
    character(*), intent(in) :: name
    real, intent(in) :: lon
    real, intent(in) :: lat
    real, intent(in) :: z

    this%name = name
    this%lon = lon
    this%lat = lat
    this%z = z
    if (.not. associated(this%records)) allocate(this%records)

  end subroutine anem_tower_init

  subroutine anem_tower_final(this)

    type(anem_tower_type), intent(inout) :: this

    if (associated(this%records)) deallocate(this%records)

  end subroutine anem_tower_final

end module anem_mod
